// pages/agreeGrade/agreeGrade.js
const app = getApp();
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreeData: {}
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 设置传过来的课程名称
    let passData = wx.getStorageSync('passData')
    this.setData({
      agreeData: passData,
    })
  },
  // 点击同意跳转函数
  agreeFn(){
    let agreeData = this.data.agreeData
    
    // console.log(agreeData)
    wx.request({
      url: app.globalData.baseUrl + '/findMyClass',
      data: {
        stu_id: wx.getStorageSync("userInfo").cluesOrStuId
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        if (res.data.code == 0) {
          let myPackList = res.data.data.myPackList,
            length = myPackList.length,
            num = 0
          myPackList.forEach(item => {
            if (agreeData.courseid == item.courseId && agreeData.noticeId == item.noticeId){
                wx.showToast({
                  title: '加载中...',
                  icon: 'loading',
                  success() {
                    wx.setStorageSync('agreeGrade', {
                      packId: agreeData.packId,
                      courseId: agreeData.courseId,
                      noticeId: agreeData.noticeId
                    })
                    let classsituation=item.classsituation
                    // if(classsituation==='undefined') return 
                    wx.reLaunch({
                      url: `/pages/period/selectPeriod/selectPeriod?showQuick=1&classsituation=${classsituation}&passData=${JSON.stringify(agreeData)}`,
                      })
                  }
                })
            } else {
              num++
            }
          })
          // console.log(num, length)
          if (num == length) {
            wx.showModal({
              title: '',
              content: '该班级已过期，无法加入，有疑问请联系管理员',
              showCancel: false,
              confirmText: '确认',
              success: function (res) {
                wx.navigateTo({
                  url: '/pages/mine/meal/meal',
                })
              }
            })
          }

        }
      }
    })
    
  },
  // 点击不同意跳转函数
  disagreeFn(){
    util.disagreeFn(this.data.agreeData.courseId)
    
  }
})